﻿/*
 * Projecto CSharp
 * Linguagens de Programacao
 * Autor: João Filipe do Nascimento Guerreiro
 * nº13730
 * Date: 26-01-2015
 * Ficheiro MainFile.cs
 * 
 */
using System;
using Excel = Microsoft.Office.Interop.Excel;
using readExcel;
using System.Windows.Forms;
using System.Collections.Generic;



namespace Excel_Sqlite
{
	/// <summary>
	/// classe principal
	/// </summary>
	class MainFile:System.Windows.Forms.Form
	{
		
		CriacaoDados bd = new CriacaoDados();
		Boolean primeiro;
		
		/// <summary>
		/// construtor
		/// </summary>
		MainFile()
		{
			InitializeComponent();
			bd.criaBaseDados();
			//boolean para indicar primeira utilização do programa
			primeiro = true;
		}
		
		/// <summary>
		/// funcao main
		/// </summary>
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.Run(new MainFile());
		}
		
		
		
		private void InitializeComponent() // DESIGNER WILL ADD THIS FUNCTION
		{
			System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
			System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend();
			this.button3 = new System.Windows.Forms.Button();
			this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.menuStrip1 = new System.Windows.Forms.MenuStrip();
			this.ficheiroToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
			this.sairToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
			this.ipc = new System.Windows.Forms.RadioButton();
			this.variacao_anual = new System.Windows.Forms.RadioButton();
			this.rmin = new System.Windows.Forms.RadioButton();
			this.rmax = new System.Windows.Forms.RadioButton();
			this.evo = new System.Windows.Forms.RadioButton();
			this.pib = new System.Windows.Forms.RadioButton();
			this.rendim_nacional = new System.Windows.Forms.RadioButton();
			this.rendim_disp = new System.Windows.Forms.RadioButton();
			this.remun_capita = new System.Windows.Forms.RadioButton();
			this.label3 = new System.Windows.Forms.Label();
			this.fontDialog1 = new System.Windows.Forms.FontDialog();
			((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
			this.menuStrip1.SuspendLayout();
			this.SuspendLayout();
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(520, 350);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(75, 23);
			this.button3.TabIndex = 2;
			this.button3.Text = "OK";
			this.button3.UseVisualStyleBackColor = true;
			this.button3.Click += new System.EventHandler(this.ConfirmaGraph);
			// 
			// chart1
			// 
			chartArea2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
			chartArea2.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.VerticalCenter;
			chartArea2.Name = "ChartArea1";
			this.chart1.ChartAreas.Add(chartArea2);
			legend2.Name = "Legend1";
			this.chart1.Legends.Add(legend2);
			this.chart1.Location = new System.Drawing.Point(215, 27);
			this.chart1.Name = "chart1";
			this.chart1.Size = new System.Drawing.Size(628, 300);
			this.chart1.TabIndex = 3;
			this.chart1.Text = "chart1";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(289, 353);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(100, 20);
			this.textBox1.TabIndex = 4;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(248, 355);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(35, 18);
			this.label1.TabIndex = 5;
			this.label1.Text = "Entre:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(395, 356);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(13, 23);
			this.label2.TabIndex = 6;
			this.label2.Text = "E:";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(414, 352);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(100, 20);
			this.textBox2.TabIndex = 7;
			// 
			// menuStrip1
			// 
			this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
			                               	this.ficheiroToolStripMenuItem});
			this.menuStrip1.Location = new System.Drawing.Point(0, 0);
			this.menuStrip1.Name = "menuStrip1";
			this.menuStrip1.Size = new System.Drawing.Size(855, 24);
			this.menuStrip1.TabIndex = 8;
			this.menuStrip1.Text = "menuStrip1";
			// 
			// ficheiroToolStripMenuItem
			// 
			this.ficheiroToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
			                                                      	this.sairToolStripMenuItem});
			this.ficheiroToolStripMenuItem.Name = "ficheiroToolStripMenuItem";
			this.ficheiroToolStripMenuItem.Size = new System.Drawing.Size(61, 20);
			this.ficheiroToolStripMenuItem.Text = "Ficheiro";
			// 
			// sairToolStripMenuItem
			// 
			this.sairToolStripMenuItem.Name = "sairToolStripMenuItem";
			this.sairToolStripMenuItem.Size = new System.Drawing.Size(93, 22);
			this.sairToolStripMenuItem.Text = "Sair";
			this.sairToolStripMenuItem.Click += new System.EventHandler(this.Sair);
			// 
			// ipc
			// 
			this.ipc.Location = new System.Drawing.Point(13, 28);
			this.ipc.Name = "ipc";
			this.ipc.Size = new System.Drawing.Size(144, 37);
			this.ipc.TabIndex = 9;
			this.ipc.TabStop = true;
			this.ipc.Text = "Indice Preço Consumidor (IPC)";
			this.ipc.UseVisualStyleBackColor = true;
			this.ipc.Click += new System.EventHandler(this.defineOp);
			// 
			// variacao_anual
			// 
			this.variacao_anual.Location = new System.Drawing.Point(12, 71);
			this.variacao_anual.Name = "variacao_anual";
			this.variacao_anual.Size = new System.Drawing.Size(132, 29);
			this.variacao_anual.TabIndex = 10;
			this.variacao_anual.TabStop = true;
			this.variacao_anual.Text = "Taxa de Inflação";
			this.variacao_anual.UseVisualStyleBackColor = true;
			this.variacao_anual.Click += new System.EventHandler(this.defineOp);
			// 
			// rmin
			// 
			this.rmin.Location = new System.Drawing.Point(12, 106);
			this.rmin.Name = "rmin";
			this.rmin.Size = new System.Drawing.Size(132, 44);
			this.rmin.TabIndex = 11;
			this.rmin.TabStop = true;
			this.rmin.Text = "Remuneração Mínima Mensal";
			this.rmin.UseVisualStyleBackColor = true;
			this.rmin.Click += new System.EventHandler(this.defineOp);
			// 
			// rmax
			// 
			this.rmax.Location = new System.Drawing.Point(12, 156);
			this.rmax.Name = "rmax";
			this.rmax.Size = new System.Drawing.Size(132, 42);
			this.rmax.TabIndex = 12;
			this.rmax.TabStop = true;
			this.rmax.Text = "Remuneracao Máxima Mensal";
			this.rmax.UseVisualStyleBackColor = true;
			this.rmax.Click += new System.EventHandler(this.defineOp);
			// 
			// evo
			// 
			this.evo.Location = new System.Drawing.Point(12, 204);
			this.evo.Name = "evo";
			this.evo.Size = new System.Drawing.Size(196, 48);
			this.evo.TabIndex = 13;
			this.evo.TabStop = true;
			this.evo.Text = "Evolução das Remunerações Mínímas e Máximas";
			this.evo.UseVisualStyleBackColor = true;
			this.evo.Click += new System.EventHandler(this.defineOp);
			// 
			// pib
			// 
			this.pib.Location = new System.Drawing.Point(12, 258);
			this.pib.Name = "pib";
			this.pib.Size = new System.Drawing.Size(173, 31);
			this.pib.TabIndex = 14;
			this.pib.TabStop = true;
			this.pib.Text = "Produto Interno Bruto Anual";
			this.pib.UseVisualStyleBackColor = true;
			this.pib.Click += new System.EventHandler(this.defineOp);
			// 
			// rendim_nacional
			// 
			this.rendim_nacional.Location = new System.Drawing.Point(11, 295);
			this.rendim_nacional.Name = "rendim_nacional";
			this.rendim_nacional.Size = new System.Drawing.Size(197, 37);
			this.rendim_nacional.TabIndex = 15;
			this.rendim_nacional.TabStop = true;
			this.rendim_nacional.Text = "Rendimento Nacional Anual bruto per capita";
			this.rendim_nacional.UseVisualStyleBackColor = true;
			this.rendim_nacional.Click += new System.EventHandler(this.defineOp);
			// 
			// rendim_disp
			// 
			this.rendim_disp.Location = new System.Drawing.Point(11, 338);
			this.rendim_disp.Name = "rendim_disp";
			this.rendim_disp.Size = new System.Drawing.Size(163, 31);
			this.rendim_disp.TabIndex = 16;
			this.rendim_disp.TabStop = true;
			this.rendim_disp.Text = "Rendimento Disponível Anual bruto per capita";
			this.rendim_disp.UseVisualStyleBackColor = true;
			this.rendim_disp.Click += new System.EventHandler(this.defineOp);
			// 
			// remun_capita
			// 
			this.remun_capita.Location = new System.Drawing.Point(11, 379);
			this.remun_capita.Name = "remun_capita";
			this.remun_capita.Size = new System.Drawing.Size(187, 24);
			this.remun_capita.TabIndex = 17;
			this.remun_capita.TabStop = true;
			this.remun_capita.Text = "Remunerações per capita anuais";
			this.remun_capita.UseVisualStyleBackColor = true;
			this.remun_capita.Click += new System.EventHandler(this.defineOp);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(248, 379);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(171, 23);
			this.label3.TabIndex = 18;
			this.label3.Text = "Insira Valores Entre: 1977-2013";
			// 
			// MainFile
			// 
			this.ClientSize = new System.Drawing.Size(855, 409);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.remun_capita);
			this.Controls.Add(this.rendim_disp);
			this.Controls.Add(this.rendim_nacional);
			this.Controls.Add(this.pib);
			this.Controls.Add(this.evo);
			this.Controls.Add(this.rmax);
			this.Controls.Add(this.rmin);
			this.Controls.Add(this.variacao_anual);
			this.Controls.Add(this.ipc);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.chart1);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.menuStrip1);
			this.MainMenuStrip = this.menuStrip1;
			this.Name = "MainFile";
			this.Text = "IPC Portugal - João Guerreiro nº13730";
			((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
			this.menuStrip1.ResumeLayout(false);
			this.menuStrip1.PerformLayout();
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.MenuStrip menuStrip1;
		private System.Windows.Forms.ToolStripMenuItem ficheiroToolStripMenuItem;
		private System.Windows.Forms.ToolStripMenuItem sairToolStripMenuItem;
		private System.Windows.Forms.RadioButton ipc;
		private System.Windows.Forms.RadioButton variacao_anual;
		private System.Windows.Forms.RadioButton rmin;
		private System.Windows.Forms.RadioButton rmax;
		private System.Windows.Forms.RadioButton evo;
		private System.Windows.Forms.RadioButton pib;
		private System.Windows.Forms.RadioButton rendim_nacional;
		private System.Windows.Forms.RadioButton rendim_disp;
		private System.Windows.Forms.RadioButton remun_capita;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.FontDialog fontDialog1;
		
		//nome da operacao escolhida
		string nome_oper;

		/// <summary>
		/// metodo que efectua a criacao do grafico com os dados inseridos pelo utilizador
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void ConfirmaGraph(object sender, EventArgs e)
		{
			//para cada grafico novo, limpar os eixos
			chart1.Series.Clear();
			
			List<long> anos;
			List<decimal> valores;
			
			int ano1=0;
			int ano2=0;

			//verifica conteudo das textbox. procura espaços em branco,e inteiros validos
			if( !string.IsNullOrWhiteSpace(textBox1.Text) && int.TryParse(textBox1.Text, out ano1))
				ano1 = int.Parse(textBox1.Text);
			
			if( !string.IsNullOrWhiteSpace(textBox2.Text) && int.TryParse(textBox2.Text, out ano2))
				ano2 = int.Parse(textBox2.Text);
			
			//verifica se o input do utilizador eh valido
			if(primeiro==false &&
			   !string.IsNullOrWhiteSpace(textBox1.Text) &&
			   !string.IsNullOrWhiteSpace(textBox2.Text) &&
			   ano1 >= 1977 &&
			   ano1 <= 2013 &&
			   ano2 >= 1977 &&
			   ano2 <= 2013 &&
			   ano1<=ano2)
			{
				Consulta c = new Consulta ();
				if (nome_oper != "evo") //evolucao das remuneracoes
				{
					chart1.Series.Add(op.Text);
					//consulta de data
					c.ReadMyData(textBox1.Text,textBox2.Text,nome_oper);
					anos = c.devolveAnos();
					valores = c.devolveValores();
					
					for (int i = 0; i < anos.Count; i++)
					{
						chart1.ChartAreas[0].RecalculateAxesScale();
						chart1.Series[op.Text].Points.AddXY(anos[i].ToString(),valores[i]);
					}
				}
				else
				{
					//caso tipo evolucao remuneracoes
					chart1.Series.Add("Remuneração Mínima Mensal");
					chart1.Series.Add("Remuneração Máxima Mensal");
					c.ReadMyData(textBox1.Text,textBox2.Text,"rmin");
					anos = c.devolveAnos();
					valores = c.devolveValores();
					for (int i = 0; i < anos.Count; i++)
					{
						chart1.ChartAreas[0].RecalculateAxesScale();
						chart1.Series["Remuneração Mínima Mensal"].Points.AddXY(anos[i].ToString(),valores[i]);
					}
					
					//segunda selecçao de valores
					anos.Clear();
					valores.Clear();
					c.ReadMyData(textBox1.Text,textBox2.Text,"rmax");
					anos = c.devolveAnos();
					valores = c.devolveValores();
					for (int i = 0; i < anos.Count; i++)
					{
						chart1.Series["Remuneração Máxima Mensal"].Points.AddXY(anos[i].ToString(),valores[i]);
					}
				}
				c.terminaConexao();
			}
		}
		
		
		RadioButton op = new RadioButton();
		
		/// <summary>
		/// define a operacao escolhida de acordo com o botao clicado
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void defineOp(object sender, EventArgs e)
		{
			primeiro=false;
			op=(RadioButton)sender;
			nome_oper=op.Name;
			
		}
		
		
		/// <summary>
		/// metodo que encerra o programa
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void Sair(object sender, EventArgs e)
		{
			bd.fechaConexao();
			Application.Exit();
		}
		

		
	}
}



